// pages/bookingClearance/bookingClearance.js
const app = getApp()
import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import { getStorage, parse } from "../../utils/utils"
import request from "../../utils/request";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    plain:true,
    item: {},
    address: {},
    stagingImages: [],
    date: '',
    dateTime: '',
    dateHalf: '',
    contactPhone:'',
    contact:'',
    isfocus:true,
    showDate:false,
    dateList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const item = parse(options.item || '{}');
    let dateList=[]
    dateList.push(this.getDateStr(0))
    dateList.push(this.getDateStr(1))
    dateList.push(this.getDateStr(2))
    this.setData({
      dateList,
      item,
      contact: getStorage('userInfo')?.nickName || '',
      contactPhone: getStorage('phone') ||'',
      isfocus:false,
    })
    this.setData({
      title: item.stagingPoint ? (item.stagingPoint + '暂存点') : '暂存点预约',
    })
  },

  // 获取后几天日期
  getDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期  
    var y = dd.getFullYear();
    var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0  
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0  
    var day = ['今天', '明天', '后天'][AddDayCount]; //天数
    var week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六', ][dd.getDay()]; //周
    // 获取时间点
    let timingList = []
    let now = new Date()
    let date = y + "/" + m + "/" + d
    let time1 = new Date(date + ' ' + '8:30')
    let time2 = new Date(date + ' ' + '11:00')
    let time3 = new Date(date + ' ' + '13:00')
    let time4 = new Date(date + ' ' + '15:00')
    let time5 = new Date(date + ' ' + '16:30:00')
    if (now < time1) {
      timingList = ['08:30-11:00', '11:00-13:00', '13:00-15:00', '15:00-16:30', '16:30-18:00']
    } else if (now < time2) {
      timingList = ['11:00-13:00', '13:00-15:00', '15:00-16:30', '16:30-18:00']
    } else if (now < time3) {
      timingList = ['13:00-15:00', '15:00-16:30', '16:30-18:00']
    } else if (now < time4) {
      timingList = ['15:00-16:30', '16:30-18:00']
    } else if (now < time5) {
      timingList = ['16:30-18:00']
    }
    console.log(y + "-" + m + "-" + d)
    return {
      day,
      week,
      date: y + "-" + m + "-" + d,
      timingList
    }
  },


  handleChangeDate(){
    this.setData({showDate:true});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 获取输入的值
   */
  handleChange(e) {
    const { address } = this.data;
    const { currentTarget = {}, detail = {} } = e;
    const { dataset: { key } } = currentTarget;
    this.setData({
      address: {
        ...address,
        [key]: detail.value
      },
      [key]: detail.value
    });
  },
  /**
   * 获取所选的日期
   */
  getSelectedDate(e) {
    const { date = '' } = e.detail;

    let dataStr = date.split(' ')
    this.setData({
      date,
      showDate: false,
      dateTime: dataStr[0],
      dateHalf: dataStr[1],
    });
  },

  changeDate(e){
    const { date = '' } = e.detail;
    let dataStr = date.split(' ')
    this.setData({
      date,
      showDate:false,
      dateTime: dataStr[0],
      dateHalf: dataStr[1],
    });
  },
  dateClose() {
    this.setData({showDate:false});
  },
  // 图片是否在上传中
   getImageLoading(e) {
    this.imageLoading = e.detail
  },
  /**
   * 可回收物/大件图片
   */
  getStagingImage(e) {
    console.log(e.detail)
    const { list = [] } = e.detail;
    this.setData({
      stagingImages: list
    });
  },
  /**
   * 提交预约单
   */
  handleSave() {
    if(!this.data.contactPhone || !this.data.contact || !this.data.date || this.data.stagingImages.length==0) return
    const { date, stagingImages,contact ,contactPhone } = this.data;
    if (!contact || !contactPhone) {
      wx.showToast({
        title: '姓名和联系方式不能为空',
        icon:'none'
      })
      return
    }
    if (contact.match(/^[ ]*$/)) {
      wx.showToast({
        title: '请输入姓名',
        icon:'none'
      })
      return
     }
    if (!(/^1[3456789]\d{9}$/.test(contactPhone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon:'none'
      })
      return
    }

    if(!date){
      wx.showToast({
        title: '请选择时间',
        icon:'none'
      })
      return
    }

    if(stagingImages && stagingImages.length === 0){
      wx.showToast({
        title: '请上传暂存点图片',
        icon:'none'
      })
      return
    }
    if (this.imageLoading) {
      wx.showToast({
        title: '图片正在上传中',
        icon: 'none'
      })
      return
    }
    
    if(this.loading) return
    this.loading=true
    this.submitFormData(stagingImages);
  },


  /**
   * 提交数据
   */
  async submitFormData(list) {
    const { date,item,contact,contactPhone  } = this.data;
    const params = {
      appointmentTime: date,
      stationAddress: item.address,
      phone: contactPhone,
      userName: contact,
      picList: list,
      stationName: item.stationName,
      stagingPoint: item.stagingPoint,
      orderType: 1,
      orderSubType: 2,
      province: item.province,
      city: item.city,
      area: item.area,
    }
    let res= await request.post(api.submitOrder, params,{loading: true})
    if (res.httpStatus === 200) {
      wx.showToast({
        title: '预约成功',
        icon: 'none'
      })
      wx.redirectTo({
        url: `/pages/bookingSuccess/bookingSuccess`,
      })
    }
    this.loading=false
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
		return shareOptions('default',e)
	},
})